package com.shanzmoo.merchant.manage.api.manager.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.base.util.CollectionUtil;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.module.product.domain.dto.ProductSaveDto;
import com.shanzmoo.db.module.product.domain.query.ProductQueryVO;
import com.shanzmoo.db.module.product.domain.vo.BrandVO;
import com.shanzmoo.db.module.product.domain.vo.ProductVO;
import com.shanzmoo.db.module.product.domain.vo.SkuResultVO;
import com.shanzmoo.db.module.product.entity.ProductBrandEntity;
import com.shanzmoo.db.module.product.entity.ProductSkuEntity;
import com.shanzmoo.db.module.product.entity.ProductSpuEntity;
import com.shanzmoo.db.module.product.service.IProductSkuService;
import com.shanzmoo.db.module.product.service.IProductSpuService;
import com.shanzmoo.merchant.manage.api.util.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 商品spumanager
 * @author xueqz
 */
@Component
@RequiredArgsConstructor
public class ProductSpuManager {

    @Autowired
    private IProductSpuService productSpuService;

    @Autowired
    private IProductSkuService productSkuService;

    public SHttpResponse<PageRespBase<ProductVO>> getSpuList(ProductQueryVO productQueryVO) {
        // 开启分页
        IPage<ProductVO> productVOIPage = productSpuService.getSpuList(productQueryVO);
        for (ProductVO productVO : productVOIPage.getRecords()) {
            if(StringUtils.isNotBlank(productVO.getCategoryName1())){
                String categoryNames = productVO.getCategoryName1();
                String[] names = categoryNames.split(",");
                if(names.length != 3) {
                    SResponse.fail(Constant.ERRCODE_PRODUCT_CATEGORYIDLENGTH, "错误商品分类级别:"+names.length);
                }
                productVO.setCategoryName1(names[0]);
                productVO.setCategoryName2(names[1]);
                productVO.setCategoryName3(names[2]);
            }else{
                SResponse.fail(Constant.ERRCODE_PRODUCT_CATEGORYNAME, "商品分类名未找到");
            }
        }
        PageRespBase<ProductVO> re = PageRespBase.create(productVOIPage, productVOIPage.getRecords());
        return SHttpResponse.ok(re);
    }

    public SHttpResponse<Boolean> operationSpu(Integer id, Boolean shelvesMark) {
        ProductSpuEntity productSpuEntity = new ProductSpuEntity();
        productSpuEntity.setId(id);
        productSpuEntity.setUpdateUser(SecurityUtil.getCurrentUserId());
        productSpuEntity.setUpdateTime(LocalDateTime.now());
        productSpuEntity.setShelves(shelvesMark);
        productSpuService.updateById(productSpuEntity);
        return SHttpResponse.ok();
    }

    /**
     * 保存spu和sku信息
     * @param productSaveDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public SHttpResponse<Boolean> saveSpu(ProductSaveDto productSaveDto) {

        return SHttpResponse.ok();
    }

    public SHttpResponse<Boolean> updateSpu(ProductSaveDto productSaveDto) {

        return SHttpResponse.ok();
    }

    public SHttpResponse<Map> getStaticProductData() {

        return SHttpResponse.ok();
    }

    public SHttpResponse<ProductSaveDto> getSpuDetailBySpuId(Integer spuId) {

        return SHttpResponse.ok();
    }
}
